using System;
using System.Collections.Generic;
using System.Windows.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Keyboard = Microsoft.Xna.Framework.Input.Keyboard;

namespace Silverlight3dApp.Learning.Lesson09
{
    public class MadnessGame : Game
    {
        private readonly Random _random = new Random();

        private Camera _camera;

        private VertexPositionTexture[] _vertexPositionTextures;

        private VertexBuffer _vertexBuffer;
        private SilverlightEffect _effect;

        private Texture2D _texture;

        private Matrix _worldTranslation = Matrix.Identity;
        private Matrix _worldRotation = Matrix.Identity;
        private Matrix _worldScale = Matrix.CreateScale(0.5f);


        private readonly List<ParticleExplosion> _explosions = new List<ParticleExplosion>();
        private readonly ParticleExplosionSettings _particleExplosionSettings = new ParticleExplosionSettings();
        private readonly ParticleSettings _particleSettings = new ParticleSettings();

        private Texture2D _explosionTexture;
        private SilverlightEffect _explosionEffect;



        protected void UpdateExplosions(GameTime gameTime)
        {
            for (var index = 0; index < _explosions.Count; index++)
            {
                var explosion = _explosions[index];
                explosion.Update(gameTime);
                if (explosion.IsDead)
                {
                    _explosions.RemoveAt(index);
                    index--;
                }
            }
        }

        protected override void Initialize()
        {
            _camera = new Camera(this, new Vector3(0, 0, 5), new Vector3(0, 0, 0), Vector3.Up);
            Components.Add(_camera);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            //_vertexPositionTextures = new VertexPositionTexture[4];
            //_vertexPositionTextures[0] = new VertexPositionTexture(new Vector3(-1, 1, 0), Vector2.Zero);
            //_vertexPositionTextures[1] = new VertexPositionTexture(new Vector3(1, 1, 0), Vector2.UnitX);
            //_vertexPositionTextures[2] = new VertexPositionTexture(new Vector3(-1, -1, 0), Vector2.UnitY);
            //_vertexPositionTextures[3] = new VertexPositionTexture(new Vector3(1, -1, 0), Vector2.One);

            //_vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionTexture.VertexDeclaration, _vertexPositionTextures.Length, BufferUsage.None);
            //_vertexBuffer.SetData(_vertexPositionTextures);

            //_texture = Content.Load<Texture2D>(@"Textures/Tree");
            //_effect = Content.Load<SilverlightEffect>(@"Effects/Red");

            _explosionTexture = Content.Load<Texture2D>(@"Textures/Particle");
            _explosionEffect = Content.Load<SilverlightEffect>(@"Effects/Particle");
            //_explosionEffect.CurrentTechnique = _explosionEffect.Techniques[0];

            _explosions.Add(new ParticleExplosion(GraphicsDevice, new Vector3(200, 200, 0), _random.Next(_particleExplosionSettings.MinLife, _particleExplosionSettings.MaxLife), _random.Next(_particleExplosionSettings.MinRoundTime, _particleExplosionSettings.MaxRoundTime), _random.Next(_particleExplosionSettings.MinParticlesPerRound, _particleExplosionSettings.MaxParticlesPerRound), _random.Next(_particleExplosionSettings.MinParticles, _particleExplosionSettings.MaxParticles), _particleSettings, _explosionEffect));

            base.LoadContent();
        }

        protected override void UnloadContent()
        {
            base.UnloadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            var keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Key.Left))
            {
                _worldTranslation *= Matrix.CreateTranslation(-0.01f, 0, 0);
            }
            if (keyState.IsKeyDown(Key.Right))
            {
                _worldTranslation *= Matrix.CreateTranslation(0.01f, 0, 0);
            }
            _worldRotation *= Matrix.CreateRotationY(MathHelper.PiOver4/60);

            UpdateExplosions(gameTime);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            if (GraphicsDevice.RasterizerState.CullMode != CullMode.None)
            {
                GraphicsDevice.RasterizerState = new RasterizerState
                                                     {
                                                         CullMode = CullMode.None,
                                                         FillMode = FillMode.WireFrame
                                                     };
            }
            GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Colors.Black, 1.0f, 0);

            //_effect.Parameters["WorldViewProjection"].SetValue(_worldRotation * _worldTranslation * _camera.View * _camera.Projection);
            //GraphicsDevice.Textures[0] = _texture;
            ////_effect.Parameters["ColoredTexture"].SetValue(_texture);

            //foreach (var pass in _effect.CurrentTechnique.Passes)
            //{
            //    pass.Apply();
            //    GraphicsDevice.SetVertexBuffer(_vertexBuffer);
            //    GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
            //}

            GraphicsDevice.Textures[0] = _explosionTexture;
            foreach (var explosion in _explosions)
            {
                explosion.Draw(_camera);
            }
            base.Draw(gameTime);
        }
    }
}